/*
 * @Author: mat
 * @Date: 2021-03-29 07:57:37
 * @LastEditTime: 2021-09-29 10:13:00
 * @Description: Cesium 初始化方法
 */

import { addLayer } from 'E/layer';

// earth 初始化
function earthInit(el) {
	Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(73, 13, 135, 53);
	let viewer = new Cesium.Viewer(el, {
		baseLayerPicker: false,
		fullscreenButton: false,
		vrButton: false,
		geocoder: false,
		animation: false,
		homeButton: false,
		infoBox: false,
		sceneModePicker: false,
		selectionIndicator: false,
		timeline: false,
		navigationHelpButton: false,
		navigationInstructionsInitiallyVisible: false,
		creditContainer: document.createElement('div'),
		imageryProvider: false,
		contextOptions: {
			webgl: {
				alpha: true,
				depth: false,
				stencil: true,
				antialias: true,
				premultipliedAlpha: true,
				preserveDrawingBuffer: true,
				failIfMajorPerformanceCaveat: true,
			},
			allowTextureFilterAnisotropic: true,
		},
		// 天空盒
		skyBox: new Cesium.SkyBox({
			sources: {
				positiveX: require('@/assets/images/skybox/tycho2t3_80_px.jpg'),
				negativeX: require('@/assets/images/skybox/tycho2t3_80_mx.jpg'),
				positiveY: require('@/assets/images/skybox/tycho2t3_80_py.jpg'),
				negativeY: require('@/assets/images/skybox/tycho2t3_80_my.jpg'),
				positiveZ: require('@/assets/images/skybox/tycho2t3_80_pz.jpg'),
				negativeZ: require('@/assets/images/skybox/tycho2t3_80_mz.jpg'),
			},
		}),
	});

	viewer.scene.debugShowFramesPerSecond = true; // 显示帧率
	viewer.scene.skyAtmosphere.show = true; // 大气层显示
	viewer.scene.fog.enabled = false; //雾效
	viewer.scene.globe.showGroundAtmosphere = false; // 地面大气效果
	window.viewer = viewer;
	setGlobalHandler();
	addLayer(g_config.map_service.base_map_layer);
}

// 设置cesium全局事件
function setGlobalHandler() {
	let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
	handler.setInputAction(function (movement) {
		let pickedObject = viewer.scene.pick(movement.position);
		if (Cesium.defined(pickedObject) && Cesium.defined(pickedObject.id) && typeof pickedObject.id.leftClick === 'function') {
			pickedObject.id.leftClick(pickedObject.id, movement);
		} else if (Cesium.defined(pickedObject) && Cesium.defined(pickedObject.primitive) && typeof pickedObject.primitive.leftClick === 'function') {
			pickedObject.primitive.leftClick(pickedObject, movement);
		}
	}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
}

export { earthInit };
